// 设置文件
import api from '@/api'
import router from "@/router";
import { Message } from 'element-ui';
const _import = require('@/libs/util.import.' + process.env.NODE_ENV)

const mutations = {
  SET_ROUTE: (state, routes) => {
    routes.forEach(item=>{
      try {
        item.component = _import(item.component);
        router.addRoute("root", item);
      } catch (error) {
        Message({
          message: `导入${item.path}路径文件错误,请拉取/创建路径文件`,
          type: 'error'
        });
        console.log('路径导入错误:', error);
      }
    })
    state.routes = routes;
  }
}

const getters = {
  routes: (state)=>{
    return state.routes;
  }
}
export default {
  namespaced: true,
  state: {
    // 路由
    routes: [],
  },
  getters,
  mutations,
  actions: {

    /**
     * @description 获取用户路由
     * @param {*} param0
     */
    async getUserRoutes({ commit }) {
      return await new Promise((resolve, reject) => {
        api.menu.getUserRoutes().then((res) => {
          commit('SET_ROUTE', res.value);
          resolve(res.value);
        }).catch((err) => {
          console.log(err);
          reject(err)
        })
      })
    }
  }
}
